/**
 * Created by tanbingqin on 2019/8/23.
 */
import React from 'react'
import { connect } from 'react-redux';
import moment from 'moment'
import {messages, deepCopy, deepFullCopy, queryCurrencyPrecision} from 'share/common'
import { Row, Col, Button, Tag } from 'antd'
import travelService from 'containers/request/travel-request/travel.service'
import errorMessage from 'share/errorMessage'

class TravelBookBtn extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false, //预订按钮的loading
      isShow: false //是否展示预订按钮
    }
  }

  componentWillMount() {
    this.initIsShow();
  };

  //初始化是否展示预订按钮
  initIsShow = () => {
    const { itinerary } = this.props;
    if (itinerary.supplierConfig
      && JSON.parse(itinerary.supplierConfig)
      && JSON.parse(itinerary.supplierConfig)[0].webReserve
      && (itinerary.bookedStatus === 2 || itinerary.bookedStatus === 3)) {
      this.setState({isShow: true});
    }
  };

  //点击预订按钮
  handleBook = () => {
    const { itinerary, type } = this.props;
    if (type === 'flight') {
      this.setState({loading: true});
      //如果是携程供应商，要判断员工是否开卡
      if (itinerary.supplierServiceName === 'supplyCtripService' || itinerary.supplierServiceName === 'vendorCtripService') {
        travelService.judgeCardEnable().then(res => {
          if (res.data && res.data.result) {
            this.getBookSSO();
          } else {
            this.setState({loading: false});
            messages('common.error');
          }
        }).catch(err => {
          this.setState({loading: false});
          errorMessage(err.response);
        });
      } else {
        this.getBookSSO();
      }
    }
    if (type === 'train') {
      this.setState({loading: true}, this.getBookSSO);
    }
    if (type === 'hotel') {
      this.setState({loading: true});
      //判断是否为甄选酒店
      if (itinerary.supplierServiceName === 'cloudheliosHotelService') {
        this.getZhenXuanHotelTicketUrl();
      } else {
        this.getBookSSO();
      }
    }
  };

  //获取预订sso
  getBookSSO = () => {
    const { itinerary, type } = this.props;
    let params = {
      supplierOID: itinerary.supplierOID,
      pageType: type === 'flight' ? 1002 : (type === 'train' ? 1010 : 1001), //1001是酒店
      businessCode: type === 'hotel' ? itinerary.approvalNumber : itinerary.approvalNum,
      itineraryDirection: itinerary.itineraryDirection
    };
    travelService.getBookTicketSSOUrl(params).then(res => {
      this.setState({loading: false});
      if (res.data && res.data.url) {
        window.open(res.data.url);
      }
    }).catch(err => {
      this.setState({loading: false});
      errorMessage(err.response);
    });
  };

  //获取甄选酒店的预订sso
  getZhenXuanHotelTicketUrl = () => {
    const { itinerary, type } = this.props;
    let params = {
      newVendorsName: 'zhenhui_hotel',
      businessCode: itinerary.approvalNumber
    };
    travelService.getHotelTicketSSOUrl(params).then(res => {
      this.setState({loading: false});
      if (res.data && res.data.url) {
        window.open(res.data.url);
      }
    }).catch(err => {
      this.setState({loading: false});
      errorMessage(err.response);
    });
  };

  render() {
    const { loading, isShow } = this.state;
    const { itinerary } = this.props;
    return (
      <div>
        {isShow && (
          <Button type="primary" onClick={this.handleBook} loading={loading} disabled={itinerary.bookedStatus === 2}>{messages('request.key649')/*预订*/}</Button>
        )}
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {}
}

TravelBookBtn.propTypes = {
  type: React.PropTypes.string,
  itinerary: React.PropTypes.object
};

TravelBookBtn.defaultProps = {
  type: '',
  itinerary: {}
};

export default connect(mapStateToProps)(TravelBookBtn)
